<?php
if (!defined("ADMREAD")) exit();

class counts
{
    var $workcount = array();
    var $newcount = array();
    var $table = '';
    var $tableid = '';
    var $total = 0;

    function level($catid = 0)
    {
        global $db,$basepref;
        $inquiry = $db->query("SELECT * FROM ".$basepref."_".$this->table."_cat WHERE parentid='$catid'");
        if ($db->numrows($inquiry) > 0) {
            while ($item = $db->fetchrow($inquiry)) {
                $count = $db->fetchrow($db->query("SELECT COUNT(".$this->tableid.") AS total FROM ".$basepref."_".$this->table."
                                                   WHERE catid='".$item['catid']."' AND act='yes'"));
                $this->total += $count['total'];
                $this->level($item['catid']);
            }
        }
        return $this->total;
    }

    function acc($cid = 0)
    {
        global $db,$basepref;
        $inquiry = $db->query("SELECT * FROM ".$basepref."_".$this->table."_cat WHERE parentid='$cid'");
        if ($db->numrows($inquiry) > 0) {
            while ($item = $db->fetchrow($inquiry)) {
                if ($item['access'] == 'user') {
                    $db->query("UPDATE ".$basepref."_".$this->table."_cat SET access='".$item['access']."',groups='".$item['groups']."' WHERE parentid='".$item['catid']."'");
                    //echo ("UPDATE ".$basepref."_".$this->table."_cat SET access='".$item['access']."',groups='".$item['groups']."' WHERE parentid='".$item['catid']."'").'<br />';
                }
                $this->acc($item['catid']);
            }
        }
    }

    function counts($table, $tableid)
    {
        global $db,$basepref;
        if (empty($table) || empty($tableid)) {
        	return false;
        }
        $this->table = $table;
        $this->tableid = $tableid;
        $inq = $db->query("SELECT catid FROM ".$basepref."_".$this->table."_cat");
        if ($db->numrows($inq) > 0) {
            while ($item = $db->fetchrow($inq)) {
                $count = $db->fetchrow($db->query("SELECT COUNT(".$this->tableid.") AS total FROM ".$basepref."_".$this->table."
                                                   WHERE catid='".$item['catid']."' AND act='yes'"));
                $this->total = 0;
                $intot = (int)($this->level($item['catid']) + $count['total']);
                $db->query("UPDATE ".$basepref."_".$this->table."_cat SET total='".$intot."' WHERE catid='".$item['catid']."'");
            }
        }
        $this->acc(0);
    }

}
?>